package org.ting.e_commerce.controller;

import cn.hutool.captcha.ShearCaptcha;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.ting.e_commerce.model.Result;
import org.ting.e_commerce.model.User;
import org.ting.e_commerce.service.UserService;
import org.ting.e_commerce.utils.EmailVerify;
import org.ting.e_commerce.utils.SecurityUtils;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/sendVerifyCode")
    public Result<?> sendEmailVerify(String email) {
        String ret = EmailVerify.sendCode(email);
        if("success".equals(ret)) {
            return Result.success(ret);
        }
        return Result.fail(ret);
    }
    @RequestMapping("/register")
    public Result<?> register(User user, Integer emailCode, HttpSession session) {
        //校验验证码
        String verify = EmailVerify.verify(user.getEmail(), emailCode);
        if(!verify.equals("success")) {
            return Result.fail(verify);
        }
        //判断用户是否已经注册
        User userByEmail = userService.selectByEmail(user.getEmail());
        if(userByEmail != null) {
            System.out.println("该邮箱已经被注册");
            return Result.fail("该邮箱已经被注册");
        }
        if(user.getPhone().length() != 11) {
            System.out.println("请输入正确的手机号");
            return Result.fail("请输入正确的手机号");
        }
        User userByPhone = userService.selectByPhone(user.getPhone());
        if(userByPhone != null) {
            System.out.println("该手机号已经被注册");
            return Result.fail("该手机号已经被注册");
        }

        //加密密码
        user.setPassword(SecurityUtils.encrypt(user.getPassword()));
        if(userService.register(user)) {
            return Result.success("success");
        }
        return Result.fail("内部错误请联系管理员");
    }
    @RequestMapping("/login")
    public Result<?> login(String email, String password, String verifyCode, HttpSession session) {
        //校验验证码
        ShearCaptcha shearCaptcha = (ShearCaptcha) session.getAttribute("verifyCode");
        if(shearCaptcha == null || !shearCaptcha.verify(verifyCode)){
            System.out.println("验证码错误");
            return Result.fail("验证码错误");
        }

        //获取用户信息
        User user = userService.selectByEmail(email);
        if(user == null) {
            System.out.println("用户还没有注册");
            return Result.fail("用户还没有注册");
        }
        if(!SecurityUtils.verity(password, user.getPassword())) {
            System.out.println("密码错误");
            return Result.fail("密码错误");
        }
        //设置session
        session.setAttribute("userId", user.getUserId());
        return Result.success(user);
    }
    @RequestMapping("/logout")
    public Result<?> logout(HttpSession session) {
        session.invalidate();
        return Result.success("success");
    }
    @RequestMapping("/selectUserById")
    public User selectUserById(Integer userId) {
        return userService.selectById(userId);
    }

    @RequestMapping("/updateUserById")
    public Result<?> updateUserById(User user){
        if(userService.updateUserById(user)) {
            return Result.success("success");
        }
        return Result.fail("内部错误");
    }
    @RequestMapping("/selectUserByEmail")
    public User selectByEmail(String email) {
        return userService.selectByEmail(email);
    }
    @RequestMapping("/updatePasswordByEmail")
    public Result<?> updatePasswordByEmail(User user, HttpSession session) {
        user.setPassword(SecurityUtils.encrypt(user.getPassword()));
        if(userService.updateUserByEmail(user)) {
            session.invalidate();
            return Result.success("success");
        }
        return Result.fail("内部错误");
    }


}
